#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        int len = flowerbed.size();
        int count = 1;
        while(count)
        {
            count = 0;
            for(int i = 3; i < len; i++)
            {
                if(flowerbed[i] == 1)
                {
                    if(flowerbed[i-1] == 0 && flowerbed[i-2] == 0 && flowerbed[i-3] == 0)
                    {
                        count++;
                        flowerbed[i-2] = 1;  // attention no so proficiency
                        n--;
                    }
                }
            }
            for(int i = len-4; i >=0; i--)  // attention, here is minus
            {
                if(flowerbed[i] == 1)
                {
                    if(flowerbed[i+1] == 0 && flowerbed[i+2] == 0 && flowerbed[i+3] == 0)
                    {
                        count++;
                        flowerbed[i+2] = 1;
                        n--;
                    }
                }
            }
        }
        if(flowerbed[0] == 0 && flowerbed[1] == 0)
        {
            flowerbed[0] = 1;
            n--;
        }    
        if(flowerbed[len-2] == 0 && flowerbed[len-1] == 0)
        {
            flowerbed[len-1] = 1;
            n--;
        }     
        if (n <= 0) return true;
        else return false;

    }
};

bool canPlaceFlowers(vector<int>& flowerbed, int n) {
    int len = flowerbed.size();
    int count = 1;
    while(count)
    {
        count = 0;
        for(int i = 3; i < len; i++)
        {
            if(flowerbed[i] == 1)
            {
                if(flowerbed[i-1] == 0 && flowerbed[i-2] == 0 && flowerbed[i-3] == 0)
                {
                    
                    count++;
                    flowerbed[i-2] = 1;
                    n--;
                }
            }
        }
        for(int i = len-4; i >=0; i--)  // attention, here is minus
        {
            if(flowerbed[i] == 1)
            {
                if(flowerbed[i+1] == 0 && flowerbed[i+2] == 0 && flowerbed[i+3] == 0)
                {
                    count++;
                    flowerbed[i+2] = 1;
                    n--;
                }
            }
        }
        for(int i = 0; i < flowerbed.size(); i++)
        {
            cout << flowerbed[i] << " ";
        }
        cout << endl;
        // cout << count << endl;
    }
    if(flowerbed[0] == 0 && flowerbed[1] == 0)
    {
        flowerbed[0] = 1;
        n--;
    }    
    if(flowerbed[len-2] == 0 && flowerbed[len-1] == 0)
    {
        flowerbed[len-1] = 1;
        n--;
    }     
    if (n <= 0) return true;
    else return false;
}
int main()
{
    vector<int> v(5);
    int len = v.size();
    int n = 2;
    bool b = false;
    v[0] = 1;
    v[1] = 0;
    v[2] = 0;
    v[3] = 0;
    v[4] = 1;
    b = canPlaceFlowers(v, n);
    cout << b;
    return 0;
}